#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$


from framework.db.mysql import BaseDb
from tornado import gen
from lib.libmd5 import md5
from lib import log

class Dorm(BaseDb):

    @gen.coroutine
    def new(self, dorm_name, school_id, remark):
        resp = ''
        ins_sql = "INSERT INTO tb_dorm(Fdorm_name, Fschool_id, Fremark, Fcreate_time) VALUES(%s,%s,%s, UNIX_TIMESTAMP())"
        try:
            sel_cur = yield self.upsert_commit(ins_sql, (dorm_name,school_id,remark))
            resp = sel_cur
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
        raise gen.Return(resp)


    @gen.coroutine
    def list(self, school_id, start, offset):
        resp = []
        sel_sql = "SELECT Fid, Fdorm_name, Fschool_id, Fremark, Fcreate_time FROM tb_dorm WHERE Fschool_id=%s ORDER BY Fid DESC LIMIT %s,%s"
        cnt_sql = "SELECT COUNT(Fid) FROM tb_dorm WHERE Fschool_id=%s"
        try:
            sel_cur = yield self.executejson(sel_sql, (school_id,start,offset))
            cnt_ret = yield self.execute(cnt_sql, (school_id,))
            # resp.append(cnt_ret)
            resp = [cnt_ret[0][0], sel_cur]
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
        raise gen.Return(resp)

    @gen.coroutine
    def edit_info(self, school_id, dorm_id):
        resp = []
        sel_sql = "SELECT Fid,Fdorm_name,Fschool_id,Fremark FROM tb_dorm WHERE Fid=%s AND Fschool_id=%s"

        try:
            sel_cur = yield self.executejson(sel_sql, (dorm_id,school_id))
            resp = sel_cur
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)

        raise gen.Return(resp)

    @gen.coroutine
    def edit(self, dorm_id, dorm_name, school_id, remark):
        resp = 0
        upd_sql = "UPDATE tb_dorm SET Fdorm_name=%s,Fremark=%s \
                    WHERE Fschool_id=%s AND Fid=%s"

        try:
            sel_cur = yield self.upsert_commit(upd_sql, (dorm_name,remark,school_id,dorm_id))
            resp = sel_cur
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
            resp = 0

        raise gen.Return(resp)


    @gen.coroutine
    def delete(self, dorm_id, school_id):
        resp = 0
        del_sql = "DELETE FROM tb_dorm WHERE Fid=%s AND Fschool_id=%s"

        try:
            del_ret = yield self.upsert_commit(del_sql, (dorm_id,school_id))
            resp = del_ret
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
            resp = 0

        raise gen.Return(resp)


    @gen.coroutine
    def search(self, dorm_name, school_id, start, offset):
        resp = []
        sel_sql = "SELECT Fid, Fdorm_name, Fschool_id, Fremark, Fcreate_time FROM tb_dorm WHERE Fschool_id=%s AND Fdorm_name = %s ORDER BY Fid DESC LIMIT %s,%s"
        cnt_sql = "SELECT COUNT(Fid) FROM tb_dorm WHERE Fschool_id=%s AND Fdorm_name = %s"
        try:
            sel_cur = yield self.executejson(sel_sql, (school_id,dorm_name,start,offset))
            cnt_ret = yield self.execute(cnt_sql, (school_id,dorm_name))
            resp.append(cnt_ret)
            resp = [cnt_ret[0][0], sel_cur]
        except Exception, e:
            log.log_exception(self.__class__.__name__, e)
        raise gen.Return(resp)


